<template>
  <div>
    <!-- 权限编辑 2018/6/5 zhangQ -->
    <Row>
      <i-col span="12">
        <Card style="width:340px;height: 450px;overflow: auto">
          菜单权限：
          <Tree :data="powerObj.menuData" show-checkbox ref="powerTree"></Tree>
        </Card>
      </i-col>
      <i-col span="12">
        <Card style="width:340px;height: 450px;overflow: auto">
          App权限：
          <CheckboxGroup v-model="selectAppDataIds">
            <template v-for="(item) in powerObj.appData">
              <Checkbox :label="item.id">
                <span>{{item.title}}</span>
              </Checkbox>
            </template>
          </CheckboxGroup>
          <br>
          <br>
          细粒度权限：
          <CheckboxGroup v-model="selectDetailsDataIds">
            <template v-for="(item) in powerObj.detailsData">
              <Checkbox :label="item.id">
                <span>{{item.title}}</span>
              </Checkbox>
            </template>
          </CheckboxGroup>
        </Card>
      </i-col>
      <!--<i-col span="8">-->
      <!--<Card style="width:320px">-->
      <!--细粒度权限：-->
      <!--</Card>-->
      <!--</i-col>-->
    </Row>


  </div>
</template>

<script>
  import _ from 'lodash'

  /**
   * TODO(组成树权限)
   * @auther zhangQ
   * @date 2018/6/5 14:55
   */
  function getTreeData (datas, parentId, roleToPower) {
    let resData = []
    datas.forEach((item) => {
      if (+item.parentId === +parentId) {
        let rpObj = _.find(roleToPower, { 'id': item.id, type: '2' })
        let resObj = {
          title: item.title,
          id: item.id,
          parentId: parentId,
          checked: false,
          expand: true
        }
        resData.push(resObj)
        let childrens = getTreeData(datas, item.id, roleToPower)
        if (childrens && childrens.length > 0) {
          resObj.children = childrens
        } else {
          resObj.checked = rpObj ? true : false
          // if (rpObj && rpObj.id) {
          //   resObj.id = rpObj.id
          // }
        }
      }
    })
    return resData
  }

  export default {
    name: 'powerEdit',
    data () {
      let selectAppDataIds = []
      let selectDetailsDataIds = []
      let powerObj = {
        menuData: [],
        appData: [],
        detailsData: []
      }
      let datas = { powerObj, selectAppDataIds, selectDetailsDataIds }
      return datas
    },
    props: {
      powerList: {
        type: Array,
        default () {
          return []
        }
      },
      subUserPowerList: {
        type: Array,
        default () {
          return []
        }
      }
    },
    computed: {},
    methods: {
      /**
       * TODO(权限改变)
       * @auther zhangQ
       * @date 2018/6/5 16:07
       */
      powerChange () {
        let powerGroupByType = _.groupBy(this.powerList, 'type')
        // 找到页面首页
        let backEndId = _.find(powerGroupByType[2], { 'name': 'backEnd' }).id
        this.powerObj.menuData = getTreeData(powerGroupByType[2], backEndId, this.subUserPowerList)
        this.powerObj.appData = powerGroupByType[3]
        this.powerObj.detailsData = powerGroupByType[4]
        // 回显app权限和细粒度权限
        let selectAppDataIds = []
        let selectDetailsDataIds = []
        this.subUserPowerList.forEach((item) => {
          if (+item.type === 3) {
            selectAppDataIds.push(+item.id)
          }
          if (+item.type === 4) {
            selectDetailsDataIds.push(+item.id)
          }
        })
        // console.log('subUserPowerList', this.subUserPowerList, selectAppDataIds, selectDetailsDataIds)
        this.selectAppDataIds = selectAppDataIds
        this.selectDetailsDataIds = selectDetailsDataIds
      },
      /**
       * TODO(获取所有选中权限)
       * @auther zhangQ
       * @date 2018/6/5 16:13
       */
      getSelectPower () {
        let menuPowers = this.$refs['powerTree'].getCheckedNodes()
        let resPowerIds = []
        menuPowers.forEach((item) => {
          if (item.id) {
            if (item.parentId) {
              resPowerIds.push(+item.parentId)
            }
            resPowerIds.push(+item.id)
          }
        })
        // let backEndId = _.find(this.powerList, { 'name': 'backEnd', type: '2' }).id
        resPowerIds = resPowerIds.concat(this.selectAppDataIds, this.selectDetailsDataIds)
        // 去重
        resPowerIds = _.uniq(resPowerIds)
        // if (backEndId) {
        //   resPowerIds.push(backEndId)
        // }
        return resPowerIds
      }
    },
    created () {
    },
    mounted () {
      this.powerChange()
      // this.powerList.forEach(() => {
      // })
    },
    components: {}
  }
</script>

<style scoped>

</style>
